package com.example.demo.controller;


import com.example.demo.bean.Goods;
import com.example.demo.bean.ShoppingCart;
import com.example.demo.bean.User;
import com.example.demo.bean.view.Result;
import com.example.demo.bean.view.ShoppingCartView;
import com.example.demo.service.ShoppingCartService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
public class ShoppingCartController {
    @Autowired(required = true)
    @Qualifier("ShoppingCartService")
    private ShoppingCartService shopCartService;

    //插入购物车
    @RequestMapping("/insertShopCart")
    @ResponseBody
    public Result<?> insertShoppingCart() {//添加一个购物车单项
        ShoppingCart shoppingCart = new ShoppingCart();
        User user = new User();
        Goods goods = new Goods();
        String ShopCartId = System.currentTimeMillis() + "";

        goods.setGoodsNum("test");
        user.setUserNum("test");
        shoppingCart.setShoppingCartNum(ShopCartId);
        shoppingCart.setBuyerNum(user.getUserNum());
        shoppingCart.setGoodsNum(goods.getGoodsNum());
        shoppingCart.setNumOfGoods(2);
        if(shopCartService.insertShoppingCart(shoppingCart))
        {
            return Result.success();
        }
        else
        {
            return Result.error("654", "添加购物车失败");
        }
    }
    @RequestMapping("/deleteShopCart")
    @ResponseBody
    public Result<?> deleteShoppingCart() {
        ShoppingCart shoppingCart = new ShoppingCart();
        shoppingCart.setShoppingCartNum("1627022896463");
        if(shopCartService.deleteShoppingCart(shoppingCart))
        {
            return Result.success();
        }
        else
        {
            return Result.error("436", "删除购物车失败");
        }
    }
    @RequestMapping("/updateShopCart")
    @ResponseBody
    //修改购物车内商品数量
    public Result<?> updateShoppingCart() {
        ShoppingCart preShoppingCart = new ShoppingCart();
        ShoppingCart laShoppingCart = new ShoppingCart();
        preShoppingCart.setShoppingCartNum("1627023234929");
        laShoppingCart.setNumOfGoods(32);
        if (shopCartService.updateShoppingCart(preShoppingCart, laShoppingCart)) {
            return Result.success();
        } else {
            return Result.error("5657", "修改购物车失败");
        }
    }
    @RequestMapping("/findAllShopCart")
    @ResponseBody
    public Result<?> findAllShoppingCart() {
        List<ShoppingCart> shoppingCartList = shopCartService.findAllShoppingCart();
        if(shoppingCartList != null)
        {
            PageHelper.startPage(1,1);
            PageInfo<ShoppingCart> info = new PageInfo<>(shoppingCartList);
            return Result.success(info);
        }
        else
        {
            return Result.error("565", "查询购物车失败");
        }
    }

    @RequestMapping("/findAllShopCartByBuyerNum")
    @ResponseBody
    public Result<?> findShoppingCartByBuyerNum() {
        User buyer = new User();
        buyer.setUserNum("test");
        List<ShoppingCartView> shoppingCartViewList = shopCartService.findShoppingCartView();
        if(shoppingCartViewList != null)
        {
            PageHelper.startPage(1,1);
            PageInfo<ShoppingCartView> info = new PageInfo<>(shoppingCartViewList);
            return Result.success(info);
        }
        else
        {
            return Result.error("545", "查询购物车失败");
        }
    }

    @RequestMapping("/findAllShopCartView")
    @ResponseBody
    public Result<?> findShoppingCartView() {
        List<ShoppingCartView> shoppingCartViewList = shopCartService.findShoppingCartView();
        if(shoppingCartViewList != null) {
            PageHelper.startPage(1,1);
            PageInfo<ShoppingCartView> info = new PageInfo<>(shoppingCartViewList);
            return Result.success(info);
        }
            else {
            return Result.error("65", "查询购物车信息失败");
        }
    }
}
